﻿using System;
using System.Data;
using NCS.Library.Components.Security;
using NCS.Module.FeedBack.Library.Components;
using NCS.Library;
using NCS.Library.Utilities;

namespace NCS.Module.FeedBack.Admin
{
	public partial class FeedBackManager : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!this.IsPostBack)
            {
                dropStatus_SelectedIndexChanged(null, null);
                this.txtEmailConfig.Text = NCS.Library.Utilities.ConvertUtility.ToString(this.Portal[NCS.Library.Definition.Portal_Feedback_ReceiveEmail + "_" + AppEnv.PortalId()]);
                this.txtSubjectEMailConfig.Text = NCS.Library.Utilities.ConvertUtility.ToString(this.Portal[NCS.Library.Definition.Portal_Feedback_SubjectEmail + "_" + AppEnv.PortalId()]);
            }
		}

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);            
        }

        private void LoadData()
        { 
            int status = 0;
            if(Int32.TryParse(this.dropStatus.SelectedValue, out status))
            {
                DataTable tbl = FeedBackController.GetFeedBacks(status);
                if (tbl != null && tbl.Rows.Count >= 0)
                {
                    this.GridView1.DataSource = tbl;
                    this.GridView1.DataBind();
                }
            }
        }

        protected void dropStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadData();
        }

        protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        {
            int id;
            if (Int32.TryParse(this.GridView1.Rows[e.RowIndex].Cells[0].Text, out id))
            {
                FeedBackController.DeleteFeedBack(id);
                this.LoadData();
            }
        }

        protected void DetailsView1_ItemCommand(object sender, System.Web.UI.WebControls.DetailsViewCommandEventArgs e)
        {
            switch (e.CommandName.ToLower())
            {                    
                case "cancel":
                    this.GridView1.SelectedIndex = -1;                    
                    //this.DetailsView1.Visible = false;
                    break;
                case "update":
                    this.GridView1.SelectedIndex = -1;
                    MessageBox.Show("Cập nhật thành công!");
                    break;
            }
        }

        protected void DetailsView1_ItemUpdated(object sender, System.Web.UI.WebControls.DetailsViewUpdatedEventArgs e)
        {
            this.LoadData();
        }

        protected void btn_UpdateEmail_Click(object sender, EventArgs e)
        {
            NCS.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Feedback_ReceiveEmail + "_" + AppEnv.PortalId(), txtEmailConfig.Text);
            NCS.Library.Components.Portal.PortalSetting.SetValue(Definition.Portal_Feedback_SubjectEmail + "_" + AppEnv.PortalId(), txtSubjectEMailConfig.Text);
        }
	}
}